package com.tan.lbs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.tan.lbs.dao.GameDAO;
import com.tan.lbs.dao.NPCDAO;
import com.tan.lbs.dao.impl.GameDAOImpl;
import com.tan.lbs.dao.impl.NPCDAOImpl;
import com.tan.lbs.entity.Game;
import com.tan.lbs.entity.GameHeader;
import com.tan.lbs.entity.NPC;
import com.tan.lbs.util.Constants;
import com.tan.lbs.util.ResourceReader;
import com.tan.lbs.util.SQLiteDatabaseHelper;
import com.tan.lbs.util.URLs;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class GamesListActivity extends ListActivity {

	@Override
	protected void onStart() {
		super.onStart();
		GameDAO gameDAO = new GameDAOImpl(this);
		List<GameHeader> remoteGames = gameDAO.findRemoteGames();
		ArrayAdapter adapter = new GameListRowAdapter(this, R.layout.gamelist_item,
				remoteGames);
		setListAdapter(adapter);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gameslist);
	}

	private class GameListRowAdapter extends ArrayAdapter<GameHeader> {

		List<GameHeader> headers;

		public GameListRowAdapter(Context context, int textViewResourceId,
				List<GameHeader> headers) {
			super(context, textViewResourceId, headers);
			this.headers = headers;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;

			if (view == null) {
				LayoutInflater vi = (LayoutInflater) getContext()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.gamelist_item, null);
			}

			final GameHeader header = headers.get(position);
			if (header != null) {
				Bitmap icon = ResourceReader.readImage(GamesListActivity.this,
						URLs.gameResourceLoc + header.getGameID() + "/"
								+ header.getIconID());

				ImageView gameIcon = (ImageView) view
						.findViewById(R.id.gameicon);
				gameIcon.setImageBitmap(icon);

				TextView gameName = (TextView) view.findViewById(R.id.gamename);
				gameName.setText(header.getGameName());
				Button button = (Button) view.findViewById(R.id.gameaction);

				String status = header.getStatus();
				if (Game.READY_STATUS.equals(status)) {
					status = getString(R.string.gamestart);
					button.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							// If the game has not started yet, change the
							// status from START
							// to READY, and populate the associated game
							// databases
							//highestAvailableQuest means the you can only check in(start) the quest when previous quests have finished.
							SharedPreferences.Editor editor = getSharedPreferences(Constants.PREFS_NAME, 0).edit();
							editor.putInt("highestAvailableQuest", 0);
							editor.putString("currentGameID", header.getGameID());
							editor.commit();

							GameDAO gameDAO = new GameDAOImpl(
									GamesListActivity.this);
							gameDAO.updateGameStatus(header.getGameID(),
									Game.START_STATUS);
							gameDAO.populdateGame(header.getGameID());
							Intent intent = new Intent();
							//intent.putExtra("intro", header.);
							intent.setClass(GamesListActivity.this,
									GameIntroActivity.class);
							GamesListActivity.this.startActivity(intent);
						}

					});
				} else if (Game.START_STATUS.equals(status)) {
					status = getString(R.string.gameresume);
					button.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							Intent intent = new Intent();
							intent.putExtra("gameID", header.getGameID());
							intent.setClass(GamesListActivity.this,
									MainPlayTabsHostActivity.class);
							GamesListActivity.this.startActivity(intent);
						}
					});
				} else if (Game.END_STATUS.equals(status)) {
					status = getString(R.string.gamereview);
				}
				button.setText(status);

			}
			return view;
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	}
}
